/* $RCSFile: ADNDynSalesOrderDetail.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/CRMWrapper3/DynEntities/ADNDynSalesOrderDetail.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:33  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.Collections;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using System.Globalization;

using CrmSdk;

namespace AddonNice.CRMWrapper
{
	public class ADNDynSalesOrderDetail : ADNDynSalesDetailBase
	{
        protected class Ftor: DFactor
        {
            public Ftor() : base(CrmStr.salesorderdetail)
            {}

            public override Type DynType
            { get { return typeof(ADNDynSalesOrderDetail); } }

            public override string DynTypename
            {   get{ return CrmStr.salesorderdetail;}}

            public override DynEntity Build()
            {   return new ADNDynSalesOrderDetail();}

            public override DynEntity Build(CRMWrapperBase w, InternalDynamicEntity dEntity, PropertyDescriptorCollection wproperties)
            {   return new ADNDynSalesOrderDetail(w, dEntity, wproperties, null);}

            public override DynEntity Build(CRMWrapperBase w, string PrimaryFieldValue)
            {   return new ADNDynSalesOrderDetail(w);}

            public override DynEntity Build(CRMWrapperBase w,Guid PrimaryKeyValue)
            {   return new ADNDynSalesOrderDetail(w,PrimaryKeyValue);}
        }

        static ADNDynSalesOrderDetail(){new Ftor();} 

        /// <summary>
		/// Reserved for template methods: do not use directly
		/// </summary>
        public ADNDynSalesOrderDetail(): base()
		{}

		public ADNDynSalesOrderDetail(CRMWrapperBase w)
			: base(w,CrmStr.salesorderdetail)
		{}
		public ADNDynSalesOrderDetail(ResetWrapperHandler fn)
			: base(fn,CrmStr.salesorderdetail)
		{}


		public ADNDynSalesOrderDetail(CRMWrapperBase w, Guid CrmId)
			: base(w,CrmStr.salesorderdetail, CrmId)
		{}
		public ADNDynSalesOrderDetail(ResetWrapperHandler fn, Guid CrmId)
			: base(fn,CrmStr.salesorderdetail, CrmId)
		{}

		public ADNDynSalesOrderDetail(CRMWrapperBase w,InternalDynamicEntity e)
			: base(w,CrmStr.salesorderdetail,e,null,null)
		{}
		public ADNDynSalesOrderDetail(ResetWrapperHandler fn,InternalDynamicEntity e)
			: base(fn,CrmStr.salesorderdetail,e,null,null)
		{}

		public ADNDynSalesOrderDetail(CRMWrapperBase w,InternalDynamicEntity e,PropertyDescriptorCollection prop,InternalColumnSet s)
			: base(w,CrmStr.salesorderdetail,e,prop,s)
		{}

		public ADNDynSalesOrderDetail(ResetWrapperHandler fn,InternalDynamicEntity e,PropertyDescriptorCollection prop,InternalColumnSet s)
			: base(fn,CrmStr.salesorderdetail,e,prop,s)
		{}

        /// <summary>
		/// Crm Key
		/// </summary>
        [CrmDynAttribute(CrmStr.salesorderdetail)]
        public Guid salesorderdetailid
		{
			get
			{
				return KeyPropertyValue(CrmStr.salesorderdetailid);
			}
			set
			{
				AddKeyProperty(CrmStr.salesorderdetailid,value);
			}
		}

        protected override void SetCrmIdProperty(Guid crmId)
        {
           if ( !dicProperty.ContainsKey(CrmStr.salesorderdetailid) || KeyPropertyValue(CrmStr.salesorderdetailid) != crmId )
                salesorderdetailid  =   crmId;
        }
 
        public override Guid PrimaryKeyGuidValue
        {
           get
           {
                return salesorderdetailid;
           }
           set
           {
               salesorderdetailid    =   value;
           }
        }

        /// <summary>
        /// Gets or sets the value that indicates if the line was copied from a quote. 
        /// </summary>
        [CrmDynAttribute(CrmStr.salesorderdetail)]
        public bool iscopied
        {
            get 
            {
                return BooleanPropertyValue(CrmStr.iscopied);
            }
            set
            {
            
                AddBooleanProperty(CrmStr.iscopied,value);
            }
        }

        [CrmDynAttribute(CrmStr.salesorderdetail)]
        public Guid salesorderid
		{
			get
			{
				return LookupPropertyValue(CrmStr.salesorderid);
			}
			set
			{
				AddLookupProperty(CrmStr.salesorderid,CrmStr.salesorder, value);
			}
		}
 
        /// <summary>
        /// Gets or sets the quantity that has been backordered for the sales order detail (order product). 
        /// </summary>
        [CrmDynAttribute(CrmStr.salesorderdetail)]
        public decimal quantitybackordered
        {
            get
            {
                return MoneyPropertyValue(CrmStr.quantitybackordered);
            }
            set
            {
                AddMoneyProperty(CrmStr.quantitybackordered,value);
            }
        }
 
        /// <summary>
        /// Gets or sets the quantity that was canceled for the sales order detail (order product). 
        /// </summary>
        [CrmDynAttribute(CrmStr.salesorderdetail)]
        public decimal quantitycancelled
        {
            get
            {
                return MoneyPropertyValue(CrmStr.quantitycancelled);
            }
            set
            {
                AddMoneyProperty(CrmStr.quantitycancelled,value);
            }
        }

        /// <summary>
        /// Gets or sets the quantity shipped for the product on the order. 
        /// </summary>
        [CrmDynAttribute(CrmStr.salesorderdetail)]
        public decimal quantityshipped
        {
            get
            {
                return MoneyPropertyValue(CrmStr.quantityshipped);
            }
            set
            {
                AddMoneyProperty(CrmStr.quantityshipped,value);
            }
        }
 
        /// <summary>
        /// Gets whether the sales order detail (order product) pricing is locked.  
        /// </summary>
        [CrmDynAttribute(CrmStr.salesorderdetail)]
        public bool salesorderispricelocked
        {
            get 
            {
                return BooleanPropertyValue(CrmStr.salesorderispricelocked);
            }
            set
            {
                AddBooleanProperty(CrmStr.salesorderispricelocked,value);
            }
        }

        
        /// <summary>
        /// Gets the status of the sales order detail (order product).
        /// </summary>
        [CrmDynAttribute(CrmStr.salesorderdetail)]
        public int salesorderstatecode
        {
            get 
            {
                return PicklistPropertyIntValue(CrmStr.salesorderstatecode);
            }
        }

        /// <summary>
        /// find  by selecting same order same product same quantity and same amount
        /// </summary>
        public bool FindByOrderProduct(ResetWrapperHandler fn,Guid orderID,Guid prodID,decimal quantity,decimal price)
        {
            if ( CRMDynTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ADNDynSalesOrderDetail FindByOrderProduct BEG orderID: {0}, prodID: {1}, quantity: {2}, price: {3}",
                    new object[]{orderID,prodID,quantity,price}),CRMDynTraceSwitch.Sw.Info);
            try
            {
                GenericDynQueryMultiple<ADNDynSalesOrderDetail> allEntQuery      =   new GenericDynQueryMultiple<ADNDynSalesOrderDetail>(wrapper,fn,CrmStr.salesorderdetail);
                InternalQueryExpression qe                          =   new InternalQueryExpression();
                qe.EntityName                                       =   CrmStr.salesorderdetail;
                InternalAllColumns RequestCols                      =   new InternalAllColumns();
                InternalFilterExpression fi                         =   new InternalFilterExpression();
                InternalConditionExpression ex                      =   new InternalConditionExpression();
                ex.AttributeName                                    =   CrmStr.salesorderid;
                ex.Operator                                         =   InternalConditionOperator.Equal;
                ex.Values                                           =   new string[]{orderID.ToString()};
                InternalConditionExpression ex1                     =   new InternalConditionExpression();
                ex1.AttributeName                                   =   CrmStr.productid;
                ex1.Operator                                        =   InternalConditionOperator.Equal;
                ex1.Values                                          =   new string[]{prodID.ToString()};
                fi.Conditions                                       =   new InternalConditionExpression[]{ex,ex1};
                qe.ColumnSet                                        =   RequestCols;
                qe.Criteria                                         =   fi;
                InternalBusinessEntityCollection allEnt             =   allEntQuery.adminRetrieve(qe);
                ADNDynSalesOrderDetail ob                           =   null;
                bool found                                          =   false;
                if ( allEntQuery.RetrievedCount > 0 )
                {
                    wrapper.Util.ExecForEachDynamicEntity(allEnt,delegate(InternalDynamicEntity de)
                        {
                            ob               =   new ADNDynSalesOrderDetail(wrapper,de,null,null);
                            if ( ob.quantity == quantity && ob.priceperunit == price )
                            {
                                wrapper.FillDicProperties(de,null,dicProperty);
                                found       =   true;
                                if ( CRMDynTraceSwitch.Sw.TraceVerbose )
                                    Trace.WriteLine( "[V]ADNDynSalesOrderDetail FindByOrderProduct found.",CRMDynTraceSwitch.Sw.Info);
                                return false; //break
                            }
                            return true;// continue
                        });
                }
                return found;
            }
            catch(Exception ex)
            {
                if ( CRMDynTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]ADNDynSalesOrderDetail FindByOrderProduct orderID: {0}, prodID: {1}, ex: {2}",
                        orderID,prodID,ex),CRMDynTraceSwitch.Sw.Info);
                throw;
            }
        }
    }
}
